package com.haidechizi.leetcode;

import java.util.List;

/**
 * @author: wangdejun
 * @create: 2020-04-10 10:05
 **/
public class _2_两数相加 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int out = 0;
        ListNode head = new ListNode(0);
        ListNode current = head;
        while (l1 != null && l2 != null) {
            int value = l1.val + l2.val + out;
            if (value / 10 == 1) {
                out = 1;
                value = value % 10;
            } else {
                out = 0;
            }
            ListNode node = new ListNode(value);
            current.next = node;
            current = current.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        while (l1 != null) {
            int value = l1.val + out;
            if (value / 10 == 1) {
                out = 1;
                value = value % 10;
            } else {
                out = 0;
            }
            l1.val = value;
            current.next = l1;
            current = current.next;
            l1 = l1.next;
        }
        while (l2 != null) {
            int value = l2.val + out;
            if (value / 10 == 1) {
                out = 1;
                value = value % 10;
            } else {
                out = 0;
            }
            l2.val = value;
            current.next = l2;
            current = current.next;
            l2 = l2.next;
        }
        if (out == 1) {
            current.next = new ListNode(out);
        }
        return head.next;
    }

    /*public ListNode revert(ListNode node) {
        if (node == null) {
            return null;
        }
        ListNode head = new ListNode(0);
        while (node != null) {
            ListNode next = node.next;
            node.next = head.next;
            head.next = node;
            node = next;
        }
        return head.next;
    }*/
}
